import random

class Player():
    def __init__(self):
        """
        Optional __init__ method is run once when your Player object is created before the
        game starts

        You can add other internal (instance) variables here at your discretion.

        You don't need to define food or reputation as instance variables, since the host
        will never use them. The host will keep track of your food and reputation for you
        as well, and return it through hunt_choices.
        """
        # Initialize variables to keep track of
        self.max_food = 0
        self.num_players = 0
        self.avg_rep = 0.3
        self.round_number = 0

    def hunt_choices(self, round_number, current_food, current_reputation, m,
            player_reputations):
        self.round_number = round_number
        if round_number == 1:
            # Useful variable to keep track of
            self.max_food = current_food
        self.num_players = len(player_reputations)
        decisions = []
        if round_number > 50:
            for rep in player_reputations:
                expected = self.expected_decision(round_number,current_reputation,rep,m)
                response = self.response(round_number,current_reputation,current_food,expected)
                decisions.append(response)
        else:
            # Try to slack as much as possible in the early stages before people
            # start to notice
            ordered = order(player_reputations)
            for i in player_reputations:
                if ordered[i] < len(player_reputations)*0.65:
                    decisions.append('s')
                else:
                    decisions.append('h')
        return decisions


    def expected_decision(self,round_number,self_rep,other_rep,m):
        """
            Tries to predict as accurately as possible the decision
            of the other player, given their reputation and the state
            of the game.
        """
        # There will be players with extreme strategies, which are easier to
        # predict this way than with a more complicated model assuming they
        # behave rationally
        if other_rep <= 0.1 and round_number > 1 or other_rep >= 0.9:
            return 0
        else:
            # In general their decision should be quite predictable by the difference
            # between our reputation and theirs. The more likely they are than us to
            # hunt, the less likely they will be to hunt against us. The more likely
            # we are to hunt than them, the more likely they'll try to slack with us.
            other_hunt_prob = self.avg_rep + (other_rep - self_rep)*0.5 + m/self.num_players/20.
            return other_hunt_prob

    def response(self,round_number,self_rep,current_food,other_hunt_prob):
        """ Given an expected decision of the other player, makes an
            appropriate response given the state of the game.
        """
        # m does not affect our decision since the effects are global
        if current_food < 100:
            return 's'
        # As food becomes more of a problem slack more
        # Some negative feedback to keep from hunting too much
        hunt_prob = other_hunt_prob - (1-current_food/float(self.max_food*15))*0.5 + (0.35 - self_rep)*0.5
        if random.random() < hunt_prob:
            return 'h'
        else:
            return 's'

    def hunt_outcomes(self, food_earnings):
        pass

    def round_end(self, award, m, number_hunters):
        # Update the fraction of players that hunt on average over all rounds so far
        if self.num_players >= 2:
            self.avg_rep = (self.avg_rep*(self.round_number-1) + float(number_hunters)/(self.num_players*(self.num_players-1)))/self.round_number
        pass
